﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BingVis.Common;

namespace BingVis.GoogleTransit
{
    public class TransitTripLoader : TransitLoaderBase<ITransitTrip>, ITransitTripLoader
    {
        private IEnumerable<ITransitStopTime> _stopTimes;

        public TransitTripLoader(IGTFSFile file)
            : this(file, new List<ITransitStopTime>())
        {

        }

        public TransitTripLoader(IGTFSFile file, IEnumerable<ITransitStopTime> stopTimes)
            : base(file)
        {
            _stopTimes = stopTimes;
        }

        public override void ProcessItem(ICSVLine line)
        {
            var dir = false;
            if ( !bool.TryParse(line["direction_id"], out dir))
                dir = false; // Redundant?

            var item = new TransitTrip(_stopTimes.Where(st => st.TripId == line["trip_id"]))
            {
                Id = line["trip_id"],
                RouteId = line["route_id"],
                BlockId = line["block_id"],
                ServiceId = line["service_id"],
                Headsign = line["trip_headsign"],
                ShortName = line["trip_short_name"],
                DirectionId = dir,
                ShapeId = line["shape_id"]
            };

            Items.Add(item);
            OnItemLoaded(item);
        }
    }
}
